package com.dearbinge.opentapi.content.shopManage;

import com.dearbinge.data.api.shopManage.ShopManageService;
import com.dearbinge.data.pojo.shopManage.ShopManage;
import com.dearbinge.data.redis.service.RedisService;
import com.dearbinge.utils.PageResult;
import com.dearbinge.utils.ResultMsg;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;
import java.util.Map;

@Api(tags = "c：店铺管理")
@RestController
@CrossOrigin
@RequestMapping("/shopManage")
@EnableAutoConfiguration
public class ShopManageController extends WebMvcConfigurerAdapter {

    @Autowired
    private ShopManageService shopManageService;

    @Autowired
    private RedisService redisService;

    /**
     * 新增/修改
     */
    @ApiOperation(value = "新增/修改",notes = "新增/修改")
    @RequestMapping(value = "/addOrUpdate",method = RequestMethod.POST)
    public ResultMsg<String> addOrUpdate(@RequestBody ShopManage shopManage){
        try {
            if (shopManage.getId() != null && shopManage.getId() != 0){
                Integer dataCode = shopManageService.update(shopManage);
                if (dataCode > 0){
                    return ResultMsg.ok("修改成功");
                }
            }else{
                ShopManage smId = shopManageService.getByUserId(shopManage.getUserId());
                if (StringUtils.isEmpty(smId)){
                    String smsCode = null;
                    if (shopManage.getMobilePhone()!= null){
                        smsCode = redisService.get(shopManage.getMobilePhone() + "smsCode");
                    }else {
                        Integer dataCode = shopManageService.insert(shopManage);
                        if (dataCode > 0) {
                            return ResultMsg.ok("添加成功");
                        }
                    }
                    if (shopManage.getSmsCode() != null && smsCode.equals(shopManage.getSmsCode())) {
                        Integer dataCode = shopManageService.insert(shopManage);
                        if (dataCode > 0) {
                            return ResultMsg.ok("添加成功");
                        }
                    }else {
                        return ResultMsg.error(500,"验证码不正确");
                    }
                }else {
                    return ResultMsg.error(500,"店铺已存在！");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据店铺编号查询
     */
    @ApiOperation(value = "根据店铺编号查询",notes = "根据店铺编号查询")
    @ApiImplicitParam(name = "shopCode",value = "店铺编号",paramType = "query")
    @RequestMapping(value = "/getByshopCode",method = RequestMethod.GET)
    public ResultMsg<ShopManage> getByshopCode(String shopCode){
        try {
            ShopManage shopManage = shopManageService.getByshopCode(shopCode,null);
            return ResultMsg.ok(shopManage);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据店铺ID删除
     */
    @ApiOperation(value = "根据店铺ID删除",notes = "根据店铺ID删除")
    @ApiImplicitParam(name = "id",value = "店铺ID",paramType = "query")
    @RequestMapping(value = "/deleted",method = RequestMethod.GET)
    public ResultMsg<String> deleted(String id){
        try {
            Integer dataCode = shopManageService.deleted(id);
            return ResultMsg.ok("删除成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据用户ID查询/可使用来做判断是否已经开店，数据为空则没开否则为已开
     */
    @ApiOperation(value = "根据用户ID查询/可使用来做判断是否已经开店，数据为空则没开否则为已开",notes = "根据用户ID查询/可使用来做判断是否已经开店，数据为空则没开否则为已开")
    @ApiImplicitParam(name = "userId",value = "用户ID",paramType = "query")
    @RequestMapping(value = "/getByUserId",method = RequestMethod.GET)
    public ResultMsg<ShopManage> getByUserId(String userId){
        try {
            ShopManage shopManage = shopManageService.getByUserId(userId);
            return ResultMsg.ok(shopManage);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据用户ID查看店铺的所有商品
     */
    @ApiOperation(value = "根据用户ID查看店铺的所有商品",notes = "根据用户ID查看店铺所有商品")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId",value = "用户ID",paramType = "query"),
            @ApiImplicitParam(name = "pageNo",value = "当前页",paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "页面数量",paramType = "query")})
    @RequestMapping(value = "/getComByUserId",method = RequestMethod.GET)
    public PageResult<List<Map<String,Object>>> getComByUserId(String userId,Integer pageNo,Integer pageSize){
        PageResult<List<Map<String,Object>>> pageResult = new PageResult<>();
        try {
            pageResult = shopManageService.getComByUserId(userId,pageNo,pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }
        return pageResult;
    }

    /**
     * 根据当前店铺ID与被拷贝店铺ID查询商品
     */
    @ApiOperation(value = "根据当前店铺ID与被拷贝店铺ID查询商品",notes = "根据当前店铺ID与被拷贝店铺ID查询商品")
    @ApiImplicitParams({@ApiImplicitParam(name = "shopId",value = "店铺ID",paramType = "query"),
            @ApiImplicitParam(name = "copyId",value = "被拷贝店铺ID",paramType = "query"),
            @ApiImplicitParam(name = "pageNo",value = "当前页",paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "页面数量",paramType = "query")})
    @RequestMapping(value = "/getNotCopyCommdityId",method = RequestMethod.GET)
    public PageResult<List<Map<String,Object>>> getNotCopyCommdityId(String shopId,String copyId,Integer pageNo,Integer pageSize){
        PageResult<List<Map<String,Object>>> pageResult = new PageResult<>();
        try {
            pageResult = shopManageService.getNotCopyCommdityId(shopId,copyId,pageNo,pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }
        return pageResult;
    }
}
